package player;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.ImageIcon;

import utilPackage.Commons;

import main.Sprite;

public class Shot extends Sprite implements Commons{

    private String shot = "../Image/shot.gif";
    private final int H_SPACE = 5;
    private final int V_SPACE = 2;
    private boolean diag;
    private int diagDirection;
    public ArrayList move_history;

    public Shot() {
    	diagDirection = 0;
    	move_history = new ArrayList();
    }

    public Shot(int x, int y) {

        ImageIcon ii = new ImageIcon(this.getClass().getResource(shot));
        setImage(ii.getImage());
        setX(x + H_SPACE);
        setY(y - V_SPACE);
        diag = false;
        diagDirection = 0;
        move_history = new ArrayList();
    }
    
    public Shot(int x, int y, boolean diagon, int direction) {

        ImageIcon ii = new ImageIcon(this.getClass().getResource(shot));
        setImage(ii.getImage());
        setX(x + H_SPACE);
        setY(y - V_SPACE);
        diag = diagon;
        if (diag) {
        	diagDirection = direction;
        }
        move_history = new ArrayList();
    }
    
    public Shot(Shot oldShot) {
    	ImageIcon ii = new ImageIcon(this.getClass().getResource(shot));
        setImage(ii.getImage());
        setX(oldShot.x);
        setY(oldShot.y);
        diag = oldShot.diag;
        diagDirection = oldShot.diagDirection;
        move_history = new ArrayList();
    }
    
    public boolean getDiag() {
    	return diag;
    }

	public int getDirection() {
		// TODO Auto-generated method stub
		return diagDirection;
	}
	
	@SuppressWarnings("unchecked")
	public void draw() {
		ArrayList tmp_move_history;
		tmp_move_history = new ArrayList();
		if (isVisible()) {

			g.drawImage(getImage(),this.getX(),this.getY(), this.imageObserver);

			try{
				Iterator i = this.move_history.iterator();
				while(i.hasNext()){
					Sprite s = (Sprite) i.next();
					s.setLife(s.getLife() -1);
					double percent_alive = (((double)(s.getLife())) / (double)((SHOT_TAIL)));
					if(s.getLife() >= 1){				
						g.setColor(new Color(0, 0, 255, Math.min(100,(int)(50 *percent_alive))));
						g.fillRect((int)(s.getX() - ((SHOT_WIDTH*percent_alive)/2) -4), s.getY(), (int)(SHOT_WIDTH+(SHOT_WIDTH*percent_alive)+8), 4);	
						tmp_move_history.add(s);
					}			
				}
				this.move_history = tmp_move_history;
				}
			catch(Exception e){
			}
		}
	}
	public void think(){
		
		this.y -= 6;
		
		if (this.getDiag()) {
			x += this.getDirection();
		}

		if (y < 0){
			this.die();
		}                    
		else{
			try{
				/*Sprite s = new Sprite();
				s.setX(this.x);
				s.setY(this.y);
				s.setLife(SHOT_TAIL);
				this.move_history.add(s);*/
			}
			catch(Exception e){
				
			}
		}
	
	}

	@Override
	public boolean doesCollide(Sprite s) {
		// TODO Auto-generated method stub
		return false;
	}

}